﻿#include "FrozenKnight.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Enums.h"
#include "../../../Shared/Data/Stat.h"
#include "../../MirEnvir/Map.h"
#include "../MapObject.h"
#include "../DelayedAction.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    FrozenKnight::FrozenKnight(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool FrozenKnight::InAttackRange()
    {
        if (getTarget()->getCurrentMap() != getCurrentMap())
        {
            return false;
        }
        if (getTarget()->getCurrentLocation() == getCurrentLocation())
        {
            return false;
        }

        int x = std::abs(getTarget()->getCurrentLocation()->X - getCurrentLocation()->X);
        int y = std::abs(getTarget()->getCurrentLocation()->Y - getCurrentLocation()->Y);

        if (x > 2 || y > 2)
        {
            return false;
        }


        return (x <= 1 && y <= 1) || (x == y || x % 2 == y % 2);
    }

    void FrozenKnight::Attack()
    {

        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));

        bool range = !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1);

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;
        ShockTime = 0;

        if (!range && getEnvir()->Random->Next(3) > 0)
        {
            S::ObjectAttack *tempVar = new S::ObjectAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            Broadcast(tempVar);
            Attack1();

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
        else
        {
            S::ObjectRangeAttack *tempVar2 = new S::ObjectRangeAttack();
            tempVar2->ObjectID = ObjectID;
            tempVar2->Direction = getDirection();
            tempVar2->Location = getCurrentLocation();
            tempVar2->TargetID = getTarget()->ObjectID;
            Broadcast(tempVar2);
            Attack2();

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
        }
    }

    void FrozenKnight::Attack1()
    {
        MirDirection dir = Functions::DirectionFromPoint(getTarget()->getCurrentLocation(), getCurrentLocation());

        dir = Functions::NextDir(dir);

        Point *target = Functions::PointMove(getCurrentLocation(), dir, 1);

        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
        if (damage == 0)
        {
            return;
        }


        Cell *cell = nullptr;

        for (int i = 0; i < 4; i++)
        {
            target = Functions::PointMove(getCurrentLocation(), dir, 1);
            dir = Functions::NextDir(dir);

            if (!getCurrentMap()->ValidPoint(target))
            {
                continue;
            }

            cell = getCurrentMap()->GetCell(target);

            if (cell->Objects.empty())
            {
                continue;
            }

            for (int o = 0; o < cell->Objects.size(); o++)
            {
                MapObject *ob = cell->Objects[o];
                if (ob->getRace() != ObjectType::Player && ob->getRace() != ObjectType::Monster)
                {
                    continue;
                }
                if (!ob->IsAttackTarget(this))
                {
                    continue;
                }

                DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 400, {ob, damage, DefenceType::MAC});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
                break;
            }
        }
    }

    void FrozenKnight::Attack2()
    {
        std::vector<MapObject*> targets = FindAllTargets(2, getTarget()->getCurrentLocation());
        if (targets.empty())
        {
            return;
        }

        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
        if (damage == 0)
        {
            return;
        }
        int delay = Functions::MaxDistance(getCurrentLocation(), getTarget()->getCurrentLocation()) * 50 + 700; //50 MS per Step
        DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + 700, {getTarget(), damage, DefenceType::AC});

        for (int i = 0; i < targets.size(); i++)
        {
            setTarget(targets[i]);
            if (getTarget()->IsAttackTarget(this))
            {
            ActionList.push_back(action);
            }
        }

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
    }
}
